package indi.wshape1.takeawaydeliveryinfo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import indi.wshape1.takeawaydeliveryinfo.pojo.Admin;
import indi.wshape1.takeawaydeliveryinfo.pojo.Group;
import indi.wshape1.takeawaydeliveryinfo.pojo.Sender;
import indi.wshape1.takeawaydeliveryinfo.pojo.TakeoutInfo;
import indi.wshape1.takeawaydeliveryinfo.pojo.response.BasicInfo;
import indi.wshape1.takeawaydeliveryinfo.pojo.response.TotalInfo;
import indi.wshape1.takeawaydeliveryinfo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Wshape1
 * @version 1.0
 * @description 处理Get请求
 * @since 2023-01-18
 */

@RestController
public class GetController {

    @Autowired
    private SenderService senderService;
    @Autowired
    private AdminService adminService;

    @Autowired
    private TakeoutInfoService takeoutInfoService;

    @Autowired
    private BlockService blockService;

    @Autowired
    private GroupService groupService;


    @GetMapping("/total/count")
    public Object getOnlineCount(HttpServletRequest request) {
        return request.getServletContext().getAttribute("onlineCount");
    }

    /**
     * query页面查询外卖信息
     *
     * @param phone
     * @return
     */
    @GetMapping("/query/data")
    public List<TakeoutInfo> query(@RequestParam String phone) {

        QueryWrapper<TakeoutInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);

        return takeoutInfoService.list(queryWrapper);
    }

    /**
     * 获取基础信息 为page页面初始化做准备
     *
     * @param request
     * @return
     */
    @GetMapping("/page/get/basic")
    public BasicInfo getBasicInfo(HttpServletRequest request) {

        String phone = (String) request.getSession().getAttribute("phone");
        String type = (String) request.getSession().getServletContext().getAttribute(phone);
        String name = "root";
        String blockName = "";
        String groupName = "";
        List<String> blocks = null;
        List<String> groups = null;


        if ("sender".equals(type)) {
            QueryWrapper<Sender> q = new QueryWrapper<>();
            q.eq("phone", phone);
            Sender sender = senderService.getOne(q);
            name = sender.getName();
            blockName = sender.getBlockName();
            groupName = sender.getGroupName();
        } else if ("admin".equals(type)) {
            QueryWrapper<Admin> qAdmin = new QueryWrapper<>();
            qAdmin.eq("phone", phone);
            Admin admin = adminService.getOne(qAdmin);
            name = admin.getName();
            blockName = admin.getBlockName();
            groups = groupService.getGroupNameByBlockName(blockName);
        } else {
            blocks = blockService.getBlockNameList();
        }


        return new BasicInfo(name, type, phone, blockName, groupName, blocks, groups);
    }

    /**
     * 获取某区域下的组别列表
     *
     * @param blockName
     * @return
     */
    @GetMapping("/page/get/group")
    public List<String> getGroupNameByBlockName(@RequestParam("blockName") String blockName) {
        return groupService.getGroupNameByBlockName(blockName);
    }

    /**
     * 根据参数进行条件限制获取信息 为page页面表格展示
     *
     * @param groupName
     * @param blockName
     * @param type
     * @param keyword
     * @return
     */
    @GetMapping("/page/get/table")
    public List<?> getTableData(@RequestParam("groupName") String groupName,
                                @RequestParam("blockName") String blockName,
                                @RequestParam("type") String type,
                                @RequestParam("keyword") String keyword) {
        groupName = "null".equalsIgnoreCase(groupName) ? null : groupName;
        blockName = "null".equalsIgnoreCase(blockName) ? null : blockName;
        List<?> ret = null;

        switch (type) {
            case "takeaway":
                ret = takeoutInfoService.getListByNameAndKeyword(blockName, groupName, keyword);
                break;
            case "sender":
                ret = senderService.getListByNameAndKeyword(blockName, groupName, keyword);
                break;
            case "admin":
                ret = adminService.getListByBlockNameAndKeyword(blockName, keyword);
                break;
            case "group":
                ret = groupService.getListByBlockNameAndKeyword(blockName, keyword);
                break;
            case "block":
                ret = blockService.getListByKeyword(keyword);
                break;
        }

        return ret;
    }

    /**
     * 获取change页面基本信息
     *
     * @param session
     * @return
     */
    @GetMapping("/change/get")
    public BasicInfo getNameAndType(HttpSession session) {
        String phone = (String) session.getAttribute("phone");
        String type = (String) session.getServletContext().getAttribute(phone);
        String name = null;

        switch (type) {
            case "sender":
                QueryWrapper<Sender> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("phone", phone);
                name = senderService.getOne(queryWrapper).getName();
                break;
            case "admin":
                QueryWrapper<Admin> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("phone", phone);
                name = adminService.getOne(queryWrapper2).getName();
                break;
        }

        BasicInfo info = new BasicInfo();
        info.setName(name);
        info.setType(type);

        return info;
    }

    /**
     * more页面获取基本统计信息
     *
     * @param session
     * @return
     */
    @GetMapping("/more/get")
    public TotalInfo getTotalInfo(HttpSession session) {
        String phone = (String) session.getAttribute("phone");
        String type = (String) session.getServletContext().getAttribute(phone);

        long takeawayCount = 0;
        long senderCount = 0;
        long adminCount = 0;
        long blockCount = 0;
        long groupCount = 0;

        switch (type) {
            case "sender":
                QueryWrapper<Sender> senderQueryWrapper = new QueryWrapper<>();
                senderQueryWrapper.eq("phone", phone);
                String groupName = senderService.getOne(senderQueryWrapper).getGroupName();
                QueryWrapper<TakeoutInfo> takeoutInfoQueryWrapper = new QueryWrapper<>();
                takeoutInfoQueryWrapper.eq("group_name", groupName);
                takeawayCount = takeoutInfoService.count(takeoutInfoQueryWrapper);
                break;
            case "admin":
                QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
                adminQueryWrapper.eq("phone", phone);
                String blockName = adminService.getOne(adminQueryWrapper).getBlockName();

                QueryWrapper<TakeoutInfo> takeoutInfoQueryWrapper1 = new QueryWrapper<>();
                takeoutInfoQueryWrapper1.eq("block_name", blockName);
                takeawayCount = takeoutInfoService.count(takeoutInfoQueryWrapper1);

                QueryWrapper<Sender> senderQueryWrapper1 = new QueryWrapper<>();
                senderQueryWrapper1.eq("block_name", blockName);
                senderCount = senderService.count(senderQueryWrapper1);

                QueryWrapper<Group> groupQueryWrapper = new QueryWrapper<>();
                groupQueryWrapper.eq("block_name", blockName);
                groupCount = groupService.count(groupQueryWrapper);
                break;
            case "root":
                takeawayCount = takeoutInfoService.count();
                senderCount = senderService.count();
                adminCount = adminService.count();
                groupCount = groupService.count();
                blockCount = blockService.count();
                break;
        }
        return new TotalInfo(phone, type, blockCount, groupCount, senderCount, adminCount, takeawayCount);
    }

    /**
     * 根据身份获取blockName list
     *
     * @param session
     * @return
     */
    @GetMapping("/more/get/block")
    public List<String> getBlockNameListInMore(HttpSession session) {

        String phone = (String) session.getAttribute("phone");
        String type = (String) session.getServletContext().getAttribute(phone);


        List<String> ret = null;

        switch (type) {
            case "root":
                ret = blockService.getBlockNameList();
                break;
            case "admin":
                QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
                adminQueryWrapper.eq("phone", phone);
                ret = new ArrayList<>(1);
                ret.add(adminService.getOne(adminQueryWrapper).getBlockName());
                break;
            case "sender":
                QueryWrapper<Sender> senderQueryWrapper = new QueryWrapper<>();
                senderQueryWrapper.eq("phone", phone);
                ret = new ArrayList<>(1);
                ret.add(senderService.getOne(senderQueryWrapper).getBlockName());
                break;
        }
        return ret;
    }

}
